/**
 * All rights Reserved, Designed By 溪云阁
 * Copyright:    Copyright(C) 2016-2020
 */

package com.module.boots.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.module.boots.exception.CommonRuntimeException;

/**
 * JSON工具类
 * @author：溪云阁
 * @date：2020年5月29日
 */

public class JsonUtils {

    /**
     * 获取json操作对象
     * @author 溪云阁
     * @return ObjectMapper
     */
    public static ObjectMapper getJSON() {
        final ObjectMapper mapper = new ObjectMapper();
        return mapper;
    }

    /**
     * 把对象转成字符串
     * @author 溪云阁
     * @param cla
     * @return String
     */
    public static String toJsonString(Object obj) {
        try {
            return getJSON().writeValueAsString(obj);
        }
        catch (final JsonProcessingException e) {
            throw new CommonRuntimeException(e.fillInStackTrace());
        }
    }

    /**
     * 把字符串转成对象
     * @author 溪云阁
     * @param content 字符串对象
     * @param valueType 对象类型
     * @return T
     */
    public static <T> T toObject(String content, Class<T> valueType) {
        try {
            return getJSON().readValue(content, valueType);
        }
        catch (final Exception e) {
            // TODO Auto-generated catch block
            throw new CommonRuntimeException(e.fillInStackTrace());
        }
    }

}
